package com.fay.designPattern.codeanalysis;

import com.google.common.collect.Lists;

import java.util.*;

/**
 * @Title:
 * @Description:
 * @Author Fay Wang
 * @Date 2022/4/21 21:18
 * @Version 1.0
 */
public class CollectionsTest {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        students.add(new Student("Alice", 19, 89.0f));
        students.add(new Student("Peter", 20, 78.0f));
        students.add(new Student("Leo", 18, 99.0f));

        Collections.sort(students, Comparator.comparingInt(o -> o.age));
        Collections.sort(students, Comparator.comparing(o -> o.name));
        Collections.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if (Math.abs(o1.score - o2.score) < 0.001) {
                    return 0;
                } else if (o1.score < o2.score) {
                    return 1;
                } else {
                    return -1;
                }
            }
        });
    }

    private static void decoratorValidation() {
        ArrayList<Integer> list = Lists.newArrayList(1, 2, 3);
        Collection<Object> newCollection = Collections.unmodifiableCollection(list);
        newCollection.add(1);
    }

    static class Student {
        private String name;
        private Integer age;
        private Float score;

        public Student(String name, Integer age, Float score) {
            this.name = name;
            this.age = age;
            this.score = score;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", score=" + score +
                    '}';
        }
    }
}
